<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="社团名称">
        <span>{{ dataForm.name }}</span>
      </el-form-item>
      <el-form-item label="负责人" prop="ownerUser">
        <span>{{ dataForm.ownerUser }}</span>
      </el-form-item>
      <el-form-item label="社团状态" prop="status">
        <el-select v-model="dataForm.status" placeholder="社团状态">
          <el-option
            v-for="item in statuss"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="挂靠学院" prop="academyName">
        <el-select v-model="dataForm.academyName" placeholder="请选择所属学院">
          <el-option
            @click.native="updateAc(item)"
            v-for="item in options"
            :key="item.label"
            :label="item.label"
            :value="item.label">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      options: [],
      statuss: [
        {value: 0, label: '待学院审批'},
        {value: 1, label: '审批通过'},
        {value: 3, label: '待审批'},
        {value: 2, label: '未通过审批'}
      ],
      dataForm: {
        id: 0,
        name: '',
        ownerId: '',
        ownerUser: '',
        status: '',
        academyId: '',
        academyName: '',
        introduce: '',
        createTime: '',
        acNum: '',
        budget: '',
        perNum: ''
      },
      dataRule: {
        name: [
          {required: true, message: '社团名称不能为空', trigger: 'blur'}
        ],
        ownerId: [
          {required: true, message: '负责人ID不能为空', trigger: 'blur'}
        ],
        ownerUser: [
          {required: true, message: '负责人姓名不能为空', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '社团状态不能为空', trigger: 'blur'}
        ],
        academyId: [
          {required: true, message: '挂靠学院ID不能为空', trigger: 'blur'}
        ],
        academyName: [
          {required: true, message: '挂靠学院不能为空', trigger: 'blur'}
        ],
        introduce: [
          {required: true, message: '社团简介不能为空', trigger: 'blur'}
        ],
        createTime: [
          {required: true, message: '成立时间不能为空', trigger: 'blur'}
        ],
        acNum: [
          {required: true, message: '活动次数不能为空', trigger: 'blur'}
        ],
        budget: [
          {required: true, message: '经费不能为空', trigger: 'blur'}
        ],
        perNum: [
          {required: true, message: '成员人数不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    updateAc (item) {
      this.dataForm.academyId = item.value
    },
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$http({
        url: this.$http.adornUrl('/aca/academy/select'),
        method: 'get',
        params: this.$http.adornParams({})
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.options = data.list
        } else {
        }
      })
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/club/clubinfo/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.clubInfo.name
              this.dataForm.ownerId = data.clubInfo.ownerId
              this.dataForm.ownerUser = data.clubInfo.ownerUser
              this.dataForm.status = data.clubInfo.status
              this.dataForm.academyId = data.clubInfo.academyId
              this.dataForm.academyName = data.clubInfo.academyName
              this.dataForm.introduce = data.clubInfo.introduce
              this.dataForm.createTime = data.clubInfo.createTime
              this.dataForm.acNum = data.clubInfo.acNum
              this.dataForm.budget = data.clubInfo.budget
              this.dataForm.perNum = data.clubInfo.perNum
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/club/clubinfo/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id,
              'academyId': this.dataForm.academyId,
              'academyName': this.dataForm.academyName,
              'status': this.dataForm.status
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
